//StudentServiceImpl.java
package org.example.demo3.service.impl;

import org.example.demo3.mapper.StudentMapper;
import org.example.demo3.model.Student;
import org.example.demo3.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service  //相当于@controller，@Mapper，告诉spring容器帮我创建一个service的对象
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    //返回学生列表
    @Override
    public List<Student> findAll() {
        return studentMapper.selectAll();
    }

    //删除学生
    @Override
    public int remove(int id){
        return studentMapper.deleteById(id);
    }

    //添加学生
    @Override
    public int add(Student student){
        return studentMapper.insert(student);
    }

    //查询对应id的学生
    @Override
    public Student findOneById(int id){
        return studentMapper.selectById(id);
    }

    //修改学生
    @Override
    public int edit(Student student) {
        return studentMapper.update(student);
    }

    //分页查询
    @Override
    public List<Student> findByPage(int currentPage,int pageSize){
        //如果页号<1 , 则从第一条记录开始，以防异常
        int formIndex = (currentPage < 1) ? 0 : (currentPage - 1) * pageSize;
        return studentMapper.selectByPage(formIndex,pageSize);
    }
    //登录验证
    @Override
    public boolean login(String username, String password) {
        //如果数据库返回不为空，则为true，否则为false
        return studentMapper.selectByNameAndPwd(username,password) != null;
    }
}

